<template>
  <div class="app-header">
    <div class="header-left">
      <!-- 折叠按钮 -->
      <div class="collapse-btn" @click="toggleSidebar">
        <el-icon :size="20">
          <Fold v-if="!isSidebarCollapsed" />
          <Expand v-else />
        </el-icon>
      </div>
      
      <!-- Logo和系统名称 -->
      <div class="logo-container">
        <div class="logo"><img class="logo-img" src="@/assets/images/erp.jpg" alt=""></div>
        <h1 class="system-name">erp业务系统</h1>
      </div>
    </div>
    
    <div class="header-right">
      <!-- 通知图标 -->
      <el-popover
        placement="bottom"
        :width="300"
        trigger="click"
        popper-class="notification-popover"
        v-model:visible="notificationVisible"
      >
        <template #reference>
      <div class="notification">
            <el-badge :value="notificationStore.unreadCount" :hidden="notificationStore.unreadCount === 0" class="notification-badge">
          <el-icon :size="20"><Bell /></el-icon>
        </el-badge>
      </div>
        </template>
        <template #default>
          <div class="notification-container">
            <div class="notification-header">
              <span>通知</span>
              <el-button link type="primary" size="small" @click="readAllNotifications" :disabled="notificationStore.unreadCount === 0">
                全部已读
              </el-button>
            </div>
            <div v-if="notificationStore.notifications.length === 0" class="empty-notifications">
              暂无通知
            </div>
            <el-scrollbar max-height="300px">
              <div 
                v-for="item in notificationStore.notifications.slice(0, 5)" 
                :key="item.id" 
                class="notification-item"
                :class="{ 'is-read': item.isRead }"
                @click="handleNotificationClick(item)"
              >
                <div class="notification-title">
                  {{ item.title }}
                  <el-tag v-if="!item.isRead" size="small" type="danger" effect="plain">未读</el-tag>
                </div>
                <div class="notification-content">{{ item.content }}</div>
                <div class="notification-time">{{ item.time }}</div>
              </div>
            </el-scrollbar>
            <div class="notification-footer">
              <el-button link type="primary" size="small" @click="viewAllNotifications">
                查看所有通知
              </el-button>
            </div>
          </div>
        </template>
      </el-popover>
      
      <!-- 全屏按钮 -->
      <div class="fullscreen" @click="toggleFullScreen">
        <el-icon :size="20">
          <FullScreen v-if="!isFullscreen" />
          <Aim v-else />
        </el-icon>
      </div>
      
      <!-- 用户信息 -->
      <el-dropdown trigger="click" @command="handleCommand">
        <div class="user-info">
          <el-avatar :size="32" :src="userAvatar" />
          <span class="username">{{ userName }}</span>
          <el-icon><ArrowDown /></el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="profile">
              <el-icon><User /></el-icon>个人中心
            </el-dropdown-item>
            <el-dropdown-item command="settings">
              <el-icon><Setting /></el-icon>系统设置
            </el-dropdown-item>
            <el-dropdown-item divided command="logout">
              <el-icon><SwitchButton /></el-icon>退出登录
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/store/auth'
import { ElMessageBox } from 'element-plus'
import { useSidebarStore } from '@/store/sidebar'
import { useNotificationStore } from '@/store/notification'

const router = useRouter()
const authStore = useAuthStore()
const sidebarStore = useSidebarStore()
const notificationStore = useNotificationStore()

// 用户信息
const userName = computed(() => authStore.name || '用户')
const userAvatar = computed(() => authStore.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png')

// 侧边栏折叠状态
const isSidebarCollapsed = computed(() => sidebarStore.isCollapsed)

// 全屏状态
const isFullscreen = ref(false)

// 使用通知store
const notifications = computed(() => notificationStore.notifications)

// 处理通知点击
const handleNotificationClick = (notification: any) => {
  // 标记为已读
  notificationStore.markAsRead(notification.id)
  
  // 如果有链接则跳转
  if (notification.link) {
    notificationVisible.value = false
    router.push(notification.link)
  }
}

// 全部已读
const readAllNotifications = () => {
  notificationStore.markAllAsRead()
}

// 查看所有通知
const viewAllNotifications = () => {
  // 首先关闭弹框 - 通过设置visible状态为false来关闭
  notificationVisible.value = false
  // 然后跳转到通知中心页面
  router.push('/notifications')
}

// 导入ref用于控制弹框显示状态
const notificationVisible = ref(false)

// 切换侧边栏折叠状态
const toggleSidebar = () => {
  sidebarStore.toggleCollapse()
}

// 切换全屏
const toggleFullScreen = () => {
  if (!document.fullscreenElement) {
    document.documentElement.requestFullscreen()
    isFullscreen.value = true
  } else {
    if (document.exitFullscreen) {
      document.exitFullscreen()
      isFullscreen.value = false
    }
  }
}

// 处理下拉菜单命令
const handleCommand = (command: string) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'settings':
      router.push('/system/settings')
      break
    case 'logout':
      ElMessageBox.confirm('确定要退出登录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        authStore.logout()
      }).catch(() => {})
      break
  }
}
</script>

<style scoped>
.app-header {
  height: var(--header-height);
  background-color: var(--header-background);
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  position: relative;
  z-index: 100;
}

.header-left {
  display: flex;
  align-items: center;
}

.collapse-btn {
  cursor: pointer;
  padding: 0 12px;
  color: var(--text-regular);
}

.collapse-btn:hover {
  color: var(--primary-color);
}

.logo-container {
  display: flex;
  align-items: center;
  margin-left: 10px;
}

.logo {
  width: 32px;
  height: 32px;
  background-color: var(--primary-color);
  border-radius: 6px;
  margin-right: 10px;
}
.logo-img{
  width: 100%;
}

.system-name {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
}

.header-right {
  display: flex;
  align-items: center;
}

.notification, .fullscreen {
  padding: 0 12px;
  cursor: pointer;
  color: var(--text-regular);
}

.notification:hover, .fullscreen:hover {
  color: var(--primary-color);
}

.notification-badge :deep(.el-badge__content) {
  top: 8px;
  right: 8px;
}

.notification-container {
  padding: 0;
}

.notification-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  border-bottom: 1px solid #eee;
}

.notification-item {
  padding: 10px 12px;
  border-bottom: 1px solid #f5f5f5;
  cursor: pointer;
  transition: background-color 0.3s;
}

.notification-item:hover {
  background-color: #f5f7fa;
}

.notification-item.is-read {
  opacity: 0.7;
}

.notification-title {
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 4px;
}

.notification-content {
  font-size: 12px;
  color: #606266;
  margin-bottom: 4px;
}

.notification-time {
  font-size: 11px;
  color: #909399;
  text-align: right;
}

.notification-footer {
  padding: 8px 12px;
  text-align: center;
  border-top: 1px solid #eee;
}

.empty-notifications {
  padding: 20px;
  text-align: center;
  color: #909399;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 0 12px;
  margin-left: 10px;
}

.username {
  margin: 0 8px;
  color: var(--text-regular);
  font-size: 14px;
}

/* 响应式 */
@media (max-width: 768px) {
  .system-name, .username {
    display: none;
  }
  
  .app-header {
    padding: 0 10px;
  }
}
</style> 